<!--
* description: 维护用车人
* author: dxuem
* createdDate: 2019-08-08
-->
<template>
  <section class="useredit">
    <el-dialog v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      @close="close"
      width="1000px"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div class="filter-container filter-params">
        <el-row>
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :dateOptionsType="comp.dateOptionsType"
            :inputType="comp.type"
            :comType="comp.comType"
            :isMul="comp.isMul"
          ></component>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/mixins/formMixins";
import { crmApis } from "@/api/apiList/crm";
import { requestAPI } from "@/api/commonRequest";
export default {
  // 组件混入对象
  mixins: [formMixins],
    components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  data() {
    return {
      // 保存API配置对象
      // apiConfig: crmAp, //车主变更审核
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存"
        }
        // {compKey: 'btnKey2', type: '', size: 'small', clickEvent: () => this.reset(), text: '重置'},
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          span: 8,
          compKey: "compKey1",
          labelName: "客户名称",
          codeField: "custName",
          textField: "custName",
          comType: "propus",
          otherField: "phone",
          isRequire: true,
          component: () =>
            import("@/components/crm/crmEjectWindows/crmCustName"),
          type: "propus",
          disabled: this.popupsState === "edit" ? true : false,
          isMust: true
        },
        {
          compKey: "compKey2",
          labelName: "电话",
          codeField: "phone",
          isRequire: false,
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "text",
          // disabled: this.formField.custClassCode==="0"?false:true,
          isMust: true,
          disabled: true
        },
        {
          compKey: "compKey3",
          labelName: "与车主关系",
          codeField: "custRelationCode",
          textField: "custRelationName",
          isRequire: true,
          lookuptype: "1016",
          component: () => import("@/components/crm/lookupValue/lookupvalue"),
          type: "text",
          disabled: this.popupsState === "edit" ? true : false,
          isMust: true
        },
        {
          compKey: "compKey4",
          labelName: "人车关系类别",
          codeField: "relationTypeCode",
          textField: "relationTypeName",
          lookuptype: "LX822",
          isRequire: true,
          component: () => import("@/components/crm/lookupValue/lookupvalue"),
          type: "dropdownList",
          disabled: this.popupsState === "edit" ? true : false,
          isMust: true
        }
      ],
      // 标题
      textMap: {
        edit: "修改使用人",
        add: "新增使用人"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        custName: "",
        custFullName: "",
        phone: "",
        custRelationCode: "",
        relationTypeCode: "",
        custRelationName: "",
        relationTypeName: "",
        dlrCustNo: "",
        custClassCode: "", //客户类型,
        vin: "",
        carBrandCode: "",
        sindex: "",
        //linkCustId:""
      },
      bcvin:"",
      bcBrand:""
    };
  },
  created(){
    var parent = this.$utils.getHasFormFieldParentComponent(this);
    let index = parent.index;
    let sindex = parent.sindex;
    this.bcvin=parent.dynamicTopic[index].vin
    this.bcBrand=parent.dynamicTopic[index].carBrandCode
  },
  methods: {
    save() {
      let that = this;
      var html = "";
      let CustNo=this.formField.dlrCustNo
      if (!this.formField.custName) {
        html = html + `<div>请选择客户名称</div>`;
      }
      if (!this.formField.custRelationCode) {
        html = html + `<div>请选择与车主关系</div>`;
      }
      if (!this.formField.relationTypeCode) {
        html = html + `<div>请选择人车关系类别</div>`;
      }
      if (html != "") {
        this.$notify.error({
          title: "校验项",
          dangerouslyUseHTMLString: true,
          message: html
        });
        return;
      }

      var parent = this.$utils.getHasFormFieldParentComponent(this);
      let index = parent.index;
      let sindex = parent.sindex;
      this.formField.vin=this.bcvin;
      this.formField.carBrandCode=this.bcBrand;
      if (1==1) {
        let params = Object.assign({}, this.formField);
        let paramsRow = {
          custName: "",
          custFullName: "",
          phone: "",
          custRelationCode: "",
          custRelationName:"",
          relationTypeCode: "",
          relationTypeName:"",
          dlrCustNo: "",
          //linkCustId:"",
          custClassCode: "" //客户类型,
        };
        for (let key in paramsRow) {
          if (params.hasOwnProperty(key)) {
            paramsRow[key] = params[key];
          }
        }
        parent.editPopupsVisible = false;
        // if (this.popupsState == "add") {
        //   parent.dynamicTopic[index].tableData.push(paramsRow);
        // } else {
        //   parent.dynamicTopic[index].tableData.splice(sindex, 1);
        //   parent.dynamicTopic[index].tableData.push(paramsRow);
        // }
      }
      if(1==1) {
        let params = Object.assign({}, this.formField);
        let paramsRow = {
          custName: "",
          custFullName: "",
          phone: "",
          custRelationCode: "",
          custRelationName:"",
          relationTypeCode: "",
          relationTypeName:"",
          dlrCustNo: "",
          //linkCustId:"",
          custClassCode: "" //客户类型,
        };
        for (let key in paramsRow) {
          if (params.hasOwnProperty(key)) {
            paramsRow[key] = params[key];
          }
        }
        this.formField.custFullName =
          this.formField.custFullName == ""
            ? this.formField.custName
            : this.formField.custFullName; //客户全称
        let ownerCustNo = this.$parent.formField.dlrCustNo;
        let carInfos = {
          carBrandCode: this.formField.carBrandCode,
          vin: this.formField.vin
        };
        if (this.popupsState == "edit") {
          let params = this.formField;
          let queryObj = {
            type: "mutation",
            apiConfig: crmApis.createdOneIdMutationSaveCust, //专营店客户信息-新增客户
            variables: {
              dataInfo: {
                custModifyScence: "303", //场景编码--4S修改客户
                custInfo: {
                  custName: this.formField.custName,
                  custFullName: this.formField.custFullName,
                  phone: this.formField.phone,
                  dlrCustNo: this.formField.dlrCustNo,
                  custClassCode: this.formField.custClassCode //客户类型
                }
              }
            }
          };
          var paramD = that.$getParams(queryObj);
          requestAPI(paramD).then(response => {
            if (response.data[queryObj.apiConfig.ServiceCode].result == "1") {
              that.$crmcf.showMessages(that, "success", "保存成功！");
              parent.editPopupsVisible = false;
            } else {
              that.$crmcf.showMessages(
                that,
                "error",
                response.data[queryObj.apiConfig.ServiceCode].msg
              );
            }
          });
        } else if (this.popupsState == "add") {
          let queryObj = {
            type: "mutation",
            apiConfig: crmApis.createdOneIdMutationSaveCust, //专营店客户信息-新增客户
            variables: {
              dataInfo: {
                custModifyScence: "304", //场景编码--4S新增用车人
                carInfo: carInfos,
                custInfo: {
                  dlrCustNo: ownerCustNo
                }, //车主客户编码
                custInfoRelation: {
                  custName: this.formField.custName,
                  custFullName: this.formField.custFullName,
                  phone: this.formField.phone,
                  custRelationCode: this.formField.custRelationCode,
                  relationTypeCode: this.formField.relationTypeCode,
                  dlrCustNo: this.formField.dlrCustNo,
                  custClassCode: this.formField.custClassCode
                }
              }
            }
          };
          var paramD = that.$getParams(queryObj);
          requestAPI(paramD).then(response => {
            if (response.data[queryObj.apiConfig.ServiceCode].result == "1") {
              that.$crmcf.showMessages(that, "success", "保存成功！");
              // parent.dynamicTopic[index].tableData.push(paramsRow);
              parent.editPopupsVisible = false;
              parent.queryadd(ownerCustNo,this.bcvin,index)
            } else {
              that.$crmcf.showMessages(
                that,
                "error",
                response.data[queryObj.apiConfig.ServiceCode].msg
              );
            }
          });
        }
      }
      // parent.editPopupsVisible = false;
    },
    close() {
      this.$parent.editPopupsVisible = false;
    }
  }
};
</script>
